package com.cheyun.netsalev3.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.bean.ClueItemData;

import java.util.List;

public class ClueItemAdapter extends RecyclerView.Adapter<ClueItemAdapter.Vh> implements View.OnClickListener {
    private Context context;
    public List<ClueItemData> stringList;

    private OnItemClickListener mItemClickListener;

    public ClueItemAdapter(Context context, List<ClueItemData> stringList) {
        this.context = context;
        this.stringList = stringList;
    }

    public ClueItemAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<ClueItemData> list) {
        this.stringList = list;
        notifyDataSetChanged();
    }

    public void refresh() {
        notifyDataSetChanged();
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    @Override
    public ClueItemAdapter.Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.clue_item, null);
        Vh holder = new Vh(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(ClueItemAdapter.Vh holder, final int position) {
        ClueItemData data = stringList.get(position);
        holder.tv.setText(data.getValue());
        holder.tn.setText(data.getName()+":");
        if (data.getKey().equals("phone")) {
            holder.tv.setTextColor(context.getResources().getColor(R.color.blue_clue));
            holder.iv.setVisibility(View.VISIBLE);
        }

        holder.itemView.setTag(data);
    }

    @Override
    public int getItemCount() {
        return stringList == null ? 0 : stringList.size();
    }

    @Override
    public void onClick(View v) {
        if (mItemClickListener == null) return;
        ClueItemData clueOperData = (ClueItemData) v.getTag();
        mItemClickListener.onItemClick(clueOperData, v);
    }


    public interface OnItemClickListener {
        void onItemClick(ClueItemData data, View v);
    }

    class Vh extends RecyclerView.ViewHolder {
        public Vh(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv_value);
            tn = itemView.findViewById(R.id.tv_name);
            iv = itemView.findViewById(R.id.iv_icon);
        }

        private TextView tv;
        private TextView tn;
        private ImageView iv;
    }
}
